﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"snake.h"
void SetPos(int x, int y)
{
	//先获取操作权限
	HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x,y };
	SetConsoleCursorPosition(hout, pos);
}
void WelcomeToGame()
{
	//x为横坐标 y为纵坐标
	SetPos(40, 14);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	SetPos(42, 20);
	system("pause");
	system("cls");
	SetPos(25, 14);
	wprintf(L"用↑ . ↓ . ← . →分别控制蛇的移动，F3为加速，F4为减速 ");
	SetPos(25, 15);
	wprintf(L"加速将能得到更高的分数");
	SetPos(42, 20);
	system("pause");
	system("cls");
}
void CreateMap()
{
	//27行 58列
	//上
	int i = 0;
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", L'□');
	}
	//下
	SetPos(0, 26);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", L'□');
	}
	//左
	for (int i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", L'□');
	}
	//右
	for (int i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", L'□');
	}
}

//初始化蛇
void InitSnake(pSnake ps)
{

	pSnakeNode cur = NULL;
	int i = 0;
	//创建蛇身坐标，并且初始化坐标
	for (int i = 0; i < 5; i++)
	{
		//创建蛇身节点
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		//设置坐标
		cur->next = NULL;
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;
		//头插法
		if (ps->head == NULL)
		{
			ps->head = cur;
		}
		else
		{
			cur->next = ps->head;
			ps->head = cur;
		}
	}
	cur = ps->head;
	while (cur)
	{
		//打印蛇的身体
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//初始化贪吃蛇的数据
	ps->sleep_time = 200;  //贪吃蛇的速度
	ps->food_weight = 10;  //一个食物的得分
	ps->score = 0;  //食物的总分
	ps->status = OK; //游戏状态
	ps->dir = right;
}

void CreateFood(pSnake ps)
{
	//先随机生成食物的坐标
	int x = 0;
	int y = 0;
	//x的坐标范围是 2~54
	// y的坐标范围是 1~25
	//x坐标必须是2的倍数
	again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	//食物的坐标不能跟蛇身的节点的坐标重复
	pSnakeNode cur = ps->head;  //获取指向蛇头的指针
	while (cur)
	{
		//判断是否和自身重复
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	//创建食物节点，打印食物
	pSnakeNode pfood = (pSnakeNode)malloc(sizeof(SnakeNode));//创建食物
	if (pfood == NULL)
	{
		perror("CreateFood::malloc()");
		return;
	}
	else
	{
		pfood->next = NULL;
		pfood->x = x;
		pfood->y = y;
		SetPos(x, y);
		wprintf(L"%lc", FOOD);
		ps->pfood = pfood;
	}
}

void GameStart(pSnake ps)
{
	//0.先设置窗口大小，再光标隐藏
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO con = { 0 };
	//获取光标的信息
	GetConsoleCursorInfo(hout, &con);
	con.bVisible = false;
	//设置光标信息
	SetConsoleCursorInfo(hout, &con);
	//getchar();
	//1.打印欢迎界面 and 功能介绍
	WelcomeToGame();
	//3.绘制地图
	CreateMap();
	//4.创建蛇
	InitSnake(ps);
	//5.创建食物
	CreateFood(ps);
}


//游戏运行

void PrintHelpInfo()
{
	//打印提示信息
	SetPos(64, 15);
	printf("不能穿墙，不能咬到自己\n");
	SetPos(64, 16);
	printf("用↑ . ↓ . ← . →分别控制蛇的移动\n");
	SetPos(64, 17);
	printf("F3 为加速， F4为减速\n");
	SetPos(64, 18);
	printf("ESC : 退出游戏, space: 暂停游戏.\n");
	SetPos(64, 20);
	printf("SYY@版权\n");
}

void pause()
{
	while (1)
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

//pSnakeNode pnext 是下一个节点的地址
//pSnake ps 维护蛇的指针
int NextIsFood(pSnakeNode pnext, pSnake ps)
{
	return ((pnext->x == ps->pfood->x) && (pnext->y == ps->pfood->y));
}

void EatFood(pSnakeNode pnext, pSnake ps)
{
	//头插法
	pnext->next = ps->head;
	ps->head = pnext;
	//打印蛇
	pSnakeNode cur = ps->head;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", FOOD);
		cur = cur->next;
	}

	//吃了食物就要加饭
	ps->score += ps->food_weight;

	//释放食物节点
	free(ps->pfood);
	ps->pfood = NULL;
	//创建新的食物
	CreateFood(ps);
}

//将下一个节点头插到蛇的身体，并将之前蛇身最后一个节点打印为空格，放弃蛇身的最后一个节点
void NoFood(pSnakeNode pnext, pSnake ps)
{
	//头插法
	pnext->next = ps->head;
	ps->head = pnext;
	//打印蛇
	pSnakeNode cur = ps->head;
	while (cur ->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//最后一个位置打印空格，然后释放节点
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;
}
void SnakeMove(pSnake ps)
{
	//创建下一个节点
	pSnakeNode pnext = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pnext == NULL)
	{
		perror("SnakeMove()::malloc");
		return;
	}
	//确定下一个节点的坐标，下一个节点的坐标根据蛇头的坐标和方向来确定
	switch (ps->dir)
	{
	case up:
	{
		pnext->x = ps->head->x;
		pnext->y = ps->head->y - 1;
	}
		break;
	case down:
	{
		pnext->x = ps->head->x;
		pnext->y = ps->head->y + 1;
	}
		break;
	case left:
	{
		pnext->x = ps->head->x - 2;  //一个节点都是2的倍数
		pnext->y = ps->head->y;
	}
		break;
	case right:
	{
		pnext->x = ps->head->x + 2;
		pnext->y = ps->head->y;
	}
		break;
	}

	//如果下一个位置就是食物
	if (NextIsFood(pnext, ps))
	{
		EatFood(pnext, ps);
	}
	else //如果没有食物
	{
		NoFood(pnext, ps);
	}
	killBYWall(ps);
	killBYSelf(ps);
}

//被墙撞死
int killBYWall(pSnake ps)
{
	if ((ps->head->x == 0) ||
		(ps->head->x == 56) ||
		(ps->head->y == 0) ||
		(ps->head->y == 26))
	{
		ps->status = KILL_BY_WALL;
		return 1;
	}
	return 0;
}
//被自己撞死
int killBYSelf(pSnake ps)
{
	pSnakeNode cur = ps->head->next;
	while (cur)
	{
		if ((ps->head->x == cur->x) && (ps->head->y == cur->y))
		{
			ps->status = KILL_BY_SELF;
			return 1;
		}
		cur = cur->next;
	}
	return 0;
}
void GameRun(pSnake ps)
{
	//右侧打印帮助信息，提示玩家
	PrintHelpInfo();
	//检测游戏状态
	do
	{
		SetPos(64, 10);
		printf("得分: %d ", ps->score);
		printf("每个食物得分:%d分", ps->food_weight);
		if (KEY_PRESS(VK_UP) && ps->dir != down)
		{
			//上
			ps->dir = up;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->dir != up)
		{
			//下
			ps->dir = down;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->dir != right)
		{
			//左
			ps->dir = left;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->dir != left)
		{
			//右
			ps->dir = right;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->status = END_NORMAL;
			break;
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->sleep_time >= 80)
			{
				ps->sleep_time -= 30;
				ps->food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->sleep_time < 350)
			{
				ps->sleep_time += 30;
				ps->food_weight -= 2;
			}
		}
		//蛇每次有休眠一定的时间，时间短，蛇移动的就快
		Sleep(ps->sleep_time);
		SnakeMove(ps);
	} while (ps->status == OK);
}

void GameEnd(pSnake ps)
{
	pSnakeNode cur = ps->head;
	SetPos(24, 12);
	switch (ps->status)
	{
	case END_NORMAL:
		printf("您主动退出游戏\n");
		break;
	case KILL_BY_SELF:
		printf("您撞上了自己了，游戏结束\n");
		break;
	case KILL_BY_WALL:
		printf("您撞墙了，游戏结束!\n");
		break;
	}

	//释放蛇的身体
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}